import axios, { AxiosResponse } from 'axios';
import Apis from '../api/Apis';

const http = axios.create({
  baseURL: 'http://172.20.10.5:7001',
  timeout: 50000
})
/* 
对返回体错误信息进行分类处理
*/
http.interceptors.response.use(
  res => res,
  error => {
    const { response } = error
    if (response) {
      const { status } = response
      if (status >= 500) {
        // 服务端报错
      } else if (status === 400) {
        // 接口参数异常
      } else if (status === 401) {
        // 登陆信息过期，需要重新登陆
      } else {
        // 其它错误类型，统一按照接口报错处理
      }
    } else {
      // 网络异常
    }
    return Promise.reject(error)
  }
)


const get = (url: string, params: any): Promise<AxiosResponse<any, any>> => {
  return http.get(url, {
    params: params,
  })
}

const post = (url: string, params: any): Promise<AxiosResponse<any, any>> => {
  return http.post(url, params);
}

export const request = (name: string, params: any): Promise<AxiosResponse<any, any>> => {
  const api = (Apis as any)[name];
  const { url, method } = api;
  if (method === 'get') {
    return get(url, params);
  } else {
    return post(url, params);
  }
}
